//  this library.
import 'package:flutter/material.dart';
import 'package:hktk_app/dao/my_dao.dart';
import 'package:hktk_app/exception/dao_exception.dart';
import 'package:hktk_app/ienum/my/asset_type_enum.dart';
import 'package:hktk_app/model/industry/exam_model.dart';
import 'package:hktk_app/model/my/asset_vo_model.dart';
import 'package:hktk_app/util/toast_util.dart';
import 'package:hktk_app/util/view_util.dart';


import 'package:hktk_app/widget/mine/bank_card_widget.dart';
import 'package:hktk_app/widget/mine/card_widget.dart';

/// 我的题库详情页面
class QuestionDetailPage extends StatefulWidget {
  /// 相关科目ids
  final String categoryIds;

  /// 考试数据
  final ExamModel exam;

  const QuestionDetailPage({
    super.key,
    required this.categoryIds,
    required this.exam,
  });

  @override
  State<QuestionDetailPage> createState() => _QuestionDetailPageState();
}

class _QuestionDetailPageState extends State<QuestionDetailPage> {
  late BuildContext _context;

  /// 初始化数据方法
  late Future<Map<String, dynamic>> _futureInit;

  /// 相关数据
  late List<AssetVoModel> _list;

  @override
  void initState() {
    super.initState();
    _list = [];
    _futureInit = _initData();
  }

  @override
  Widget build(BuildContext context) {

    _context = context;
    return FutureBuilder<Map<String, dynamic>>(
      future: _futureInit,
      builder: (
          BuildContext context,
          AsyncSnapshot<Map<String, dynamic>> snapshot,
          ) {
        if (snapshot.connectionState == ConnectionState.done) {
          Map<String, dynamic> map = snapshot.data!;
          if (!map["suc"]) {
            WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
              ToastUtil.show(context: context, msg: map["msg"]);
            });
          }
          return _content();
        } else {
          return const Center(
            child: CircularProgressIndicator(),
          );
        }
      },
    );
  }

  /// 初始化数据方法
  Future<Map<String, dynamic>> _initData() async {
    Map<String, dynamic> map = {};
    try {
      _list = await MyDao.myBuyAsset(
        type: AssetTypeEnum.question,
        categoryIds: widget.categoryIds,
      );
      map["suc"] = true;
    } catch (e) {
      map = {"suc": false, "msg": "未知错误，请重试"};
      if (e is DaoException) {
        map["msg"] = e.msg;
      }
    }
    return map;
  }

  /// 内容显示方法
  Widget _content() {
    Widget widget;
    if (_list.isNotEmpty) {
      widget = MediaQuery.removePadding(
        removeTop: true,
        context: _context,
        child: ListView.separated(
          itemBuilder: _itemBuilder,
          separatorBuilder: _separatorBuilder,
          itemCount: _list.length + 1,
        ),
      );
    } else {
      widget = showNoData();
    }
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 24),
      child: widget,
    );
  }

  /// 内容显示
  Widget? _itemBuilder(BuildContext context, int index) {
    if (index < _list.length) {
      AssetVoModel model = _list[index];
      return CardWidget(
        efficientStatus: model.efficientStatus!,
        child: BankCardWidget(
          model: model,
          type: AssetTypeEnum.question,
          exam: widget.exam,
        ),
      );

    } else {
      // 提示组件
      return showListEnd();
    }
  }

  /// 间隔组件
  Widget _separatorBuilder(BuildContext context, int index) {
    return hiSpace(height: 20);
  }
}
